#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long LL;
const int N = 2e6 + 10;

void solve()
{
    int n, k, l, r;
    cin >> n >> k >> l >> r;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }
    map<int, int> cnt1, cnt2;
    int dis1 = 0, dis2 = 0;
    int left1 = 0, left2 = 0;
    int ans = 0;
    for (int right = 0; right < n; ++right)
    {
        if (cnt1[a[right]] == 0)
            dis1++;
        cnt1[a[right]]++;
        while (dis1 > k)
        {
            cnt1[a[left1]]--;
            if (cnt1[a[left1]] == 0)
                dis1--;
            left1++;
        }
        if (cnt2[a[right]] == 0)
            dis2++;
        cnt2[a[right]]++;
        while (dis2 > k - 1)
        {
            cnt2[a[left2]]--;
            if (cnt2[a[left2]] == 0)
                dis2--;
            left2++;
        }
        int ll = left1;
        int rr = left2 - 1;
        if (ll > rr)
            continue;
        int lowerb = max(ll, right - r + 1);
        int upperb = min(rr, right - l + 1);
        if (lowerb <= upperb)
        {
            ans += upperb - lowerb + 1;
        }
    }
    cout << ans << '\n';
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
}